package com.xzz.dp;

/**
 * @author: hhz
 * @create: 2021-12-30 14:35
 * 583. 两个字符串的删除操作
 * 给定两个单词 word1 和 word2，找到使得 word1 和 word2 相同所需的最小步数，每步可以删除任意一个字符串中的一个字符。
 *
 * 输入: "sea", "eat"
 * 输出: 2
 * 解释: 第一步将"sea"变为"ea"，第二步将"eat"变为"ea"
 **/
public class MinDistance {
    public static void main(String[] args) {
        System.out.println(minDistance("sea","eat"));

    }
    public static int minDistance(String word1, String word2) {
        int lsc = LCS(word1,word2);
        return word1.length()-lsc+word2.length()-lsc;
    }
    public static int LCS(String s1,String s2){
        int m = s1.length();
        int n = s2.length();
        int[][] dp = new int[m+1][n+1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if(s1.charAt(i-1)==s2.charAt(j-1)){
                    dp[i][j] = 1+dp[i-1][j-1];
                }else {
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[m][n];
    }
}
